<template>
  <div>
    <el-dialog :title="info.isAdd?'管理员添加':'管理员编辑'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        user:{{user}}
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="user.roleid">
            <el-option
              v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="user.password" autocomplete="off" show-password></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
// import { IndexRoutes } from "../../../router";
import { formClose } from "../../../mixins";
import {
  requseradd,
  requserinfo,
  requseredit,
  reqrolelist
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";

export default {
  props: ["info", "arr"],
  mixins: [formClose],

  data() {
    return {
      user: {
        roleid: "",
        username: "",
        password: "",
        status: 1
      },
      //角色的列表
      roleList: []
    };
  },
  computed: {
    ...mapGetters({
      userInfo: "userInfo"
    })
  },
  mounted() {
    reqrolelist().then(res => {
      if (res.data.code === 200) {
        this.roleList = res.data.list ? res.data.list : [];
      }
    });
  },
  methods: {
    ...mapActions({
      changeUserInfo:"changeUserInfo"

    }),
    cancel() {
      this.info.isshow = false;
    },
    empty() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status: 1
      };
    },
    add() {
      requseradd(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框内容清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求
          this.$emit("init");
        }
      });
    },

    //获取一条数据
    getOne(id) {
      requserinfo({ uid: id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          //单独处理密码
          this.user.password = "";
        }
      });
    },
    update() {
      requseredit(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //如果user.uid=userInfo.uid清空用户数据，跳转登录页
          if (this.user.uid === this.userInfo.uid) {
            this.changeUserInfo({});
            this.$router.push("/login");
            return;
          }

          //弹框内容清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求
          this.$emit("init");
        }
      });
    },
    getTree() {
      //获取选中key的数组
      console.log(this.$refs.tree.getCheckedKeys());
    },
    close() {
      if (!this.info.isAdd) {
        this.empty();
      }
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
</style>